package com.store.satring.dos.aa;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.Transient;
import javax.persistence.Version;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

@Configurable
@Entity
public class Users {

    /**
     */
    @NotNull
    @Column(unique = true)
    @Size(min = 2, max = 20)
    private String loginId;

    /**
     */
    @NotNull
    @Column(unique = true)
    @Size(min = 2, max = 20)
    private String name;

    /**
     */
    private String emailId;

    /**
     */
    private String mobileNo;

    /**
     */
    private Boolean disabled;

    /**
     */
    @NotNull
    @Size(min = 4, max = 200)
    private String password;

    /**
     */
//    @NotNull
    @Size(min = 4, max = 20)
    @Transient
    private transient String rePassword;

	@Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private Long id;

	@Version
    @Column(name = "version")
    private Integer version;

	public Long getId() {
        return this.id;
    }

	public void setId(Long id) {
        this.id = id;
    }

	public Integer getVersion() {
        return this.version;
    }

	public void setVersion(Integer version) {
        this.version = version;
    }

	public String getLoginId() {
        return this.loginId;
    }

	public void setLoginId(String loginId) {
        this.loginId = loginId;
    }

	public String getName() {
        return this.name;
    }

	public void setName(String name) {
        this.name = name;
    }

	public String getEmailId() {
        return this.emailId;
    }

	public void setEmailId(String emailId) {
        this.emailId = emailId;
    }

	public String getMobileNo() {
        return this.mobileNo;
    }

	public void setMobileNo(String mobileNo) {
        this.mobileNo = mobileNo;
    }

	public Boolean getDisabled() {
        return this.disabled;
    }

	public void setDisabled(Boolean disabled) {
        this.disabled = disabled;
    }

	public String getPassword() {
        return this.password;
    }

	public void setPassword(String password) {
        this.password = password;
    }

	public String toString() {
        return ReflectionToStringBuilder.toString(this, ToStringStyle.SHORT_PREFIX_STYLE);
    }

	@PersistenceContext
    transient EntityManager entityManager;

	public static final List<String> fieldNames4OrderClauseFilter = java.util.Arrays.asList("loginId", "name", "emailId", "mobileNo", "disabled", "password");

	public static final EntityManager entityManager() {
        EntityManager em = new Users().entityManager;
        if (em == null) throw new IllegalStateException("Entity manager has not been injected (is the Spring Aspects JAR configured as an AJC/AJDT aspects library?)");
        return em;
    }

	public static long countUserses() {
        return entityManager().createQuery("SELECT COUNT(o) FROM Users o", Long.class).getSingleResult();
    }

	public static List<Users> findAllUserses() {
        return entityManager().createQuery("SELECT o FROM Users o", Users.class).getResultList();
    }

	public static List<Users> findAllUserses(String sortFieldName, String sortOrder) {
        String jpaQuery = "SELECT o FROM Users o";
        if (fieldNames4OrderClauseFilter.contains(sortFieldName)) {
            jpaQuery = jpaQuery + " ORDER BY " + sortFieldName;
            if ("ASC".equalsIgnoreCase(sortOrder) || "DESC".equalsIgnoreCase(sortOrder)) {
                jpaQuery = jpaQuery + " " + sortOrder;
            }
        }
        return entityManager().createQuery(jpaQuery, Users.class).getResultList();
    }

	public static Users findUsers(Long id) {
        if (id == null) return null;
        return entityManager().find(Users.class, id);
    }

	public static List<Users> findUsersEntries(int firstResult, int maxResults) {
        return entityManager().createQuery("SELECT o FROM Users o", Users.class).setFirstResult(firstResult).setMaxResults(maxResults).getResultList();
    }

	public static Users findUser(String loginId) {
        Query createQuery = entityManager().createNativeQuery("SELECT id FROM Users where login_Id = '"+loginId+"'");
        Object singleResult = null;
        try{
		singleResult = createQuery.getSingleResult();
        }catch(Exception exp){
        	exp.printStackTrace();
        }
		if(singleResult != null){
			return findUsers(Long.valueOf(singleResult.toString()));
		}
		return null;
    }

	public static List<Users> findUsersEntries(int firstResult, int maxResults, String sortFieldName, String sortOrder) {
        String jpaQuery = "SELECT o FROM Users o";
        if (fieldNames4OrderClauseFilter.contains(sortFieldName)) {
            jpaQuery = jpaQuery + " ORDER BY " + sortFieldName;
            if ("ASC".equalsIgnoreCase(sortOrder) || "DESC".equalsIgnoreCase(sortOrder)) {
                jpaQuery = jpaQuery + " " + sortOrder;
            }
        }
        return entityManager().createQuery(jpaQuery, Users.class).setFirstResult(firstResult).setMaxResults(maxResults).getResultList();
    }

	@Transactional
    public void persist() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.persist(this);
    }

	@Transactional
    public void remove() {
        if (this.entityManager == null) this.entityManager = entityManager();
        if (this.entityManager.contains(this)) {
            this.entityManager.remove(this);
        } else {
            Users attached = Users.findUsers(this.id);
            this.entityManager.remove(attached);
        }
    }

	@Transactional
    public void flush() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.flush();
    }

	@Transactional
    public void clear() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.clear();
    }

	@Transactional
    public Users merge() {
        if (this.entityManager == null) this.entityManager = entityManager();
        Users merged = this.entityManager.merge(this);
        this.entityManager.flush();
        return merged;
    }

	public String getRePassword() {
		return rePassword;
	}

	public void setRePassword(String rePassword) {
		this.rePassword = rePassword;
	}
}
